package androidlab.sprintingsquirrel.game;

import static org.andengine.extension.physics.box2d.util.constants.PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT;

import org.andengine.engine.camera.BoundCamera;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

import androidlab.sprintingsquirrel.ResourceManager;

public class Monster extends AnimatedSprite{

	private Body body;
	private int leftContacts;
	private int rightContacts;
	private boolean direction;
	final long[] PLAYER_ANIMATE = new long[] { 50, 50, 50 };
	
	public Monster(float pX, float pY, VertexBufferObjectManager vbo, BoundCamera camera, PhysicsWorld physicsWorld) {
		super(pX, pY, ResourceManager.getInstance().monsterTR, vbo);
		createPhysics(camera, physicsWorld);
		leftContacts = 0;
		rightContacts = 0;
		direction = true;
	}

	private void createPhysics(final BoundCamera camera, PhysicsWorld physicsWorld) {
	    body = PhysicsFactory.createBoxBody(physicsWorld, this, BodyType.DynamicBody, PhysicsFactory.createFixtureDef(0f, 0f, 0f));

	    body.setUserData("monster");
	    body.setFixedRotation(true);
	    body.setBullet(true);
	    
	    final PolygonShape polyLeft = new PolygonShape();
	    polyLeft.setAsBox(2f/PIXEL_TO_METER_RATIO_DEFAULT, 2f/PIXEL_TO_METER_RATIO_DEFAULT, new Vector2(-24f/PIXEL_TO_METER_RATIO_DEFAULT, 32f/PIXEL_TO_METER_RATIO_DEFAULT), 0);
	    
	    FixtureDef fixtureDefLeft = PhysicsFactory.createFixtureDef(0f, 0f, 0f, true);
	    fixtureDefLeft.shape = polyLeft;
	    
	    Fixture feetLeft = body.createFixture(fixtureDefLeft);
	    feetLeft.setUserData("feetLeft");
	    polyLeft.dispose();
	    
	    final PolygonShape polyRight = new PolygonShape();
	    polyRight.setAsBox(2f/PIXEL_TO_METER_RATIO_DEFAULT, 2f/PIXEL_TO_METER_RATIO_DEFAULT, new Vector2(24f/PIXEL_TO_METER_RATIO_DEFAULT, 32f/PIXEL_TO_METER_RATIO_DEFAULT), 0);
	    
	    FixtureDef fixtureDefRight = PhysicsFactory.createFixtureDef(0f, 0f, 0f, true);
	    fixtureDefRight.shape = polyRight;
	    
	    Fixture feetRight = body.createFixture(fixtureDefRight);
	    feetRight.setUserData("feetRight");
	    polyRight.dispose();
	    
	    final PolygonShape polyLeftHead = new PolygonShape();
	    polyLeftHead.setAsBox(2f/PIXEL_TO_METER_RATIO_DEFAULT, 2f/PIXEL_TO_METER_RATIO_DEFAULT, new Vector2(-24f/PIXEL_TO_METER_RATIO_DEFAULT, 0f/PIXEL_TO_METER_RATIO_DEFAULT), 0);
	    
	    FixtureDef fixtureDefLeftHead = PhysicsFactory.createFixtureDef(0f, 0f, 0f, true);
	    fixtureDefLeftHead.shape = polyLeftHead;
	    
	    Fixture feetLeftHead = body.createFixture(fixtureDefLeftHead);
	    feetLeftHead.setUserData("feetLeftHead");
	    polyLeftHead.dispose();
	    
	    final PolygonShape polyRightHead = new PolygonShape();
	    polyRightHead.setAsBox(2f/PIXEL_TO_METER_RATIO_DEFAULT, 2f/PIXEL_TO_METER_RATIO_DEFAULT, new Vector2(24f/PIXEL_TO_METER_RATIO_DEFAULT, 0f/PIXEL_TO_METER_RATIO_DEFAULT), 0);
	    
	    FixtureDef fixtureDefRightHead = PhysicsFactory.createFixtureDef(0f, 0f, 0f, true);
	    fixtureDefRightHead.shape = polyRightHead;
	    
	    Fixture feetRightHead = body.createFixture(fixtureDefRightHead);
	    feetRightHead.setUserData("feetRightHead");
	    polyRightHead.dispose();
	    
	    physicsWorld.registerPhysicsConnector(new PhysicsConnector(this, body, true, false) {
	        @Override
	        public void onUpdate(float pSecondsElapsed) {
	            super.onUpdate(pSecondsElapsed);
	            camera.onUpdate(0.1f);
	        }
	    });
	}
	
	public void run(float xValue) {
		if(direction) {
			body.setLinearVelocity(new Vector2(xValue*12, body.getLinearVelocity().y));
		}
		else {
			body.setLinearVelocity(new Vector2(-xValue*12, body.getLinearVelocity().y));
		}
	}
	
	public Body getBody() {
		return body;
	}
	
	public void addLeftContacts() {
		this.leftContacts++;
	}
	
	public void addRightContacts() {
		this.rightContacts++;
	}
	
	public void removeLeftContacts() {
		this.leftContacts--;
		if(leftContacts == 0) {
			direction = true;
			animate(PLAYER_ANIMATE, 0, 2,  true);
		}
	}
	
	public void removeRightContacts() {
		this.rightContacts--;
		if(rightContacts == 0) {
			direction = false;
			animate(PLAYER_ANIMATE, 5, 7,  true);
		}
	}
	
	public void setDirection(boolean direction) {
		this.direction = direction;
		if(direction) {
			animate(PLAYER_ANIMATE, 0, 2,  true);
		}
		else {
			animate(PLAYER_ANIMATE, 5, 7,  true);
		}
	}
	
	@Override
    protected void onManagedUpdate(float pSecondsElapsed) {
        super.onManagedUpdate(pSecondsElapsed);
        this.run(0.2f);
    }
}
